package com.linhh.mall.center.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linhh.mall.center.api.BusinessInfoService;
import com.linhh.mall.center.common.pojo.BusinessInfo;
import com.linhh.mall.center.common.pojo.ShopInfo;
import com.linhh.mall.center.common.pojo.UserInfo;
import com.linhh.mall.center.server.mappers.BusinessInfoMapper;
import com.linhh.mall.center.server.mappers.ShopInfoMapper;
import com.linhh.mall.center.server.mappers.UserInfoMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @Author linhh
 * @Date 2022 12 11 23 53
 **/
@Service
public class BusinessInfoServiceImpl implements BusinessInfoService {

    @Autowired
    private BusinessInfoMapper businessInfoMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private ShopInfoMapper shopInfoMapper;
    @Override
    public PageInfo<BusinessInfo> getBusinessList(Integer pageNo, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNo,pageSize);
        Example example = new Example(BusinessInfo.class);
        if(status >= 0){
            example.createCriteria().andEqualTo("status",status);
        }
        List<BusinessInfo> businessInfoList = businessInfoMapper.selectByExample(example);
        PageInfo<BusinessInfo> pageInfo = new PageInfo<>(businessInfoList);
        return pageInfo;
    }

    @Override
    public int selectCompNameExist(String compName) {
        Example example = new Example(BusinessInfo.class);
        example.createCriteria().andEqualTo("compName",compName);
        return businessInfoMapper.selectCountByExample(example);
    }

    @Override
    public int shopNameIsExist(String shopName) {
        Example example = new Example(ShopInfo.class);
        example.createCriteria().andEqualTo("shopName",shopName);
        return shopInfoMapper.selectCountByExample(example);
    }

    @Override
    public int shopUserNameIsExist(String shopUserName) {
        Example example = new Example(UserInfo.class);
        example.createCriteria().andEqualTo("userName",shopUserName);
        return userInfoMapper.selectCountByExample(example);
    }

    @Override
    public int register(BusinessInfo businessInfo) {
        // 添加商家信息
        businessInfo.setDeleteFlag((byte)0);
        businessInfo.setCreateTime(new Date());
        businessInfo.setUpdateTime(new Date());
        int result = businessInfoMapper.insertUseGeneratedKeys(businessInfo);

        // 获取最新的主键ID,也是最新的商家ID
        Long businessId = businessInfo.getId();
        ShopInfo shopInfo = businessInfo.getShopInfo();

        // 设置商家ID
        shopInfo.setBusinessId(businessId);
        shopInfo.setDeleteFlag((byte)0);
        shopInfo.setCreateTime(new Date());
        shopInfo.setUpdateTime(new Date());

        // 添加商品信息
        shopInfoMapper.insertUseGeneratedKeys(shopInfo);

        // 获取当前商品ID
        Long shopId = shopInfo.getId();

        UserInfo userInfo = businessInfo.getUserInfo();
        userInfo.setShopId(shopId);

        // 加密用户密码
        userInfo.setUserPassword(new BCryptPasswordEncoder().encode(userInfo.getUserPassword()));
        userInfoMapper.insertSelective(userInfo);
        return result;
    }

    @Override
    public int updateBusinessInfo(BusinessInfo businessInfo) {
        businessInfoMapper.updateByPrimaryKey(businessInfo);
        return 1;
    }
}
